    /* 
  author : Harjeet

*/

package mailuser;

import java.io.UnsupportedEncodingException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

// ...

public class mailUsers
{
    public static void main(String args[])
    {
        mailUsers m2=new mailUsers();
        for(int i=0;i<1;i++)
        {
        m2.sendmail("Hello user acc activated","harjeet.singh321@gmail.com");
        System.out.print("\n Done" + i);
        }
        
    }
    public void sendmail(String bodytext,String useremailid)
    {
            Properties props = new Properties();
            String host = "smtp.gmail.com";
            final String from = "admin@harjeetsingh.in";
            final String pass ="kjsclicks" ;
            String port="465";
            props.put("mail.smtp.starttls.enable", "true"); // added this line
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.user", from);
            props.put("mail.smtp.password",pass);
            props.put("mail.smtp.port", 587);
            props.put("mail.smtp.auth", "true");
        //Session session = Session.getDefaultInstance(props, null);
        Session session  = Session.getDefaultInstance( props , new javax.mail.Authenticator() 
    {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(
                            from, pass);// Specify the Username and the PassWord
                }
            });

        String msgBody = bodytext;

        try {
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress("admin@harjeetsingh.in", "Admin"));
            msg.addRecipient(Message.RecipientType.TO,
                             new InternetAddress(useremailid, "Hello User"));
            msg.setSubject("Message from Evoting System");
            msg.setText(msgBody);
            //Transport transport = session.getTransport("smtp");            
             
            Transport.send(msg);
            

        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(mailUsers.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        } catch (AddressException e) {
            // ...
            e.printStackTrace();
        } catch (MessagingException e) {
            // ...
            e.printStackTrace();
        }
    }
    
    
        
}
